package com.wasp.collector.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

import org.quartz.Job;

import com.google.common.collect.Lists;
import com.wasp.collector.timer.JobEntity;
import com.wasp.collector.timer.QuartzManager;

public class PropertiesUtil {
	private static final String QUARTZTIME= "collector.quertz.time";
	private static final String QUARTZCLASS= "collector.quertz.class";
	private static final String QUARTZTRIGGERNAME= "collector.quertz.triggername";
	private static final String QUARTZJOBNAME= "collector.quertz.jobname";
	
			
	private static Properties properties = null;
	private static Object objLock = new Object();
	public static Properties loadProperies() {
		// 获取propreties文件中的相关属性
		if(properties == null){
			synchronized (objLock) {
				if(properties == null){
					
					try {
						InputStream in = new BufferedInputStream(
								new FileInputStream(PropertiesUtil.class.getResource("/").getPath() + "collector.properties"));
						properties = new Properties();
						properties.load(in);

						in.close();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		}
		return properties;
	}
	
	public static List<JobEntity> loadJobs() throws ResourceException{
		Properties p = loadProperies();
		List<Object> quartzs = p.keySet().stream().filter(item->isQuartz(item))
			.collect(Collectors.toList());
		if(quartzs.size()%4 != 0){
			throw new ResourceException(String.format("%s %s %s %s need!",QUARTZTIME,
					QUARTZCLASS,QUARTZJOBNAME,QUARTZTRIGGERNAME));
		}
		List<JobEntity> list = Lists.newArrayList();
		int quartzCount = quartzs.size()/4;
		for(int i = 0;i<quartzCount;i++){
			JobEntity j = new JobEntity();
			String className =p.getProperty(String.format(QUARTZCLASS+"[%d]", i)); 
			j.setClassz(createJob(className));
			j.setConExpression(p.getProperty(String.format(QUARTZTIME+"[%d]", i)));
			j.setJobName(p.getProperty(String.format(QUARTZJOBNAME+"[%d]", i)));
			j.setTriggerName(p.getProperty(String.format(QUARTZTRIGGERNAME+"[%d]", i)));
			list.add(j);
		}
		return list;
	}
	private static boolean isQuartz(Object item){
		return item.toString().startsWith("collector.quertz");
	}
	public static boolean AkkaCluserServer(){
		Properties p = loadProperies();
		return "true".equals(p.getProperty("akka.cluster.server").trim());
	}
	public static boolean AkkaCluserClient(){
		Properties p = loadProperies();
		return "true".equals(p.getProperty("akka.cluster.client").trim());
	}
	public static String[] AkkaCluserServerPort(){
		Properties p = loadProperies();
		String portStr = p.getProperty("akka.cluster.server.port").trim();
		if(StringUtils.isNullOrEmpty(portStr)){
			return new String[]{"0"};
		}
		String[] ports = portStr.split(",");
		return ports;
	}
	
	
	public static void main(String[] args) throws Exception {
		/*List<JobEntity> jobs =  loadJobs();
		if(jobs !=null){
			jobs.stream().forEach(action->System.out.println( action.getJobName()));
		}*/
		QuartzManager.build();
	}
	private  static <T extends Job> Class<T> createJob(String className){
		try{
           Class<T> jobs =  (Class<T>) Class.forName(className);
           return  jobs;
        }catch (Exception e) {
            e.printStackTrace();
        }
		return null;
	}
}
